import type { RouteRecordRaw } from 'vue-router'

// 获取modules中的路由规则
const modules = import.meta.glob('./modules/**/*.ts', { eager: true })

// modules路由规则
const routes: RouteRecordRaw[] = []

Object.keys(modules).forEach((key) => {
  const modulesRoutes = (modules[key] as { default?: RouteRecordRaw | RouteRecordRaw[] }).default

  let modRoutesList: RouteRecordRaw[] = []
  if (Array.isArray(modulesRoutes)) {
    modRoutesList = [...modulesRoutes]
  } else if (modulesRoutes && typeof modulesRoutes === 'object' && Object.keys(modulesRoutes).length > 0) {
    // 确保modulesRoutes是有效的RouteRecordRaw对象
    modRoutesList = [modulesRoutes as RouteRecordRaw]
  }
  // 如果modulesRoutes是空对象或无效值，则忽略不添加

  routes.push(...modRoutesList)
})

// 根目录路由规则
const rootRoute: RouteRecordRaw = {
  path: '/',
  name: 'root',
  redirect: '/home'
}

// 404页面路由规则
const notFoundPage: RouteRecordRaw = {
  path: '/:pathMatch(.*)*',
  name: '404',
  component: () => import('@/views/404Page.vue')
}

export default [rootRoute, ...routes, notFoundPage]